import urllib.request
import random

# 使用urllib获取百度首页源码

baidu_url = 'https://www.baidu.com/s?ie=UTF-8&wd=ip'

headers = {
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) '
                  'Chrome/118.0.0.0 Safari/537.36 Edg/118.0.2088.76',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-',
    'Cookie': 'BAIDUID=F1CB82512F626E5BB8EC0A85AFD49E3A:FG=1; BIDUPSID=F1CB82512F626E5BB8EC0A85AFD49E3A; PSTM=1693642461; BD_UPN=123253; BDUSS=TNGVjUzTXBBSkdmbS1aYmdKLVpDdXI3SG5NWmtmdn5zOTlVeDh1eWxTNG43QzlsSVFBQUFBJCQAAAAAAAAAAAEAAABTJZLP087OqtPOz7e94su1AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACdfCGUnXwhlW; BDUSS_BFESS=TNGVjUzTXBBSkdmbS1aYmdKLVpDdXI3SG5NWmtmdn5zOTlVeDh1eWxTNG43QzlsSVFBQUFBJCQAAAAAAAAAAAEAAABTJZLP087OqtPOz7e94su1AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACdfCGUnXwhlW; H_WISE_SIDS=234020_110085_268593_269904_271172_270102_275170_276533_277030_277355_277640_275733_277997_259642_278056_278392_278575_278704_278514_278388_279021_279044_279357_279613_279286_279711_279998_280302_280375_278415_276438_280619_280809_280106_280557_278791_280636_203517_267170_281037_280167_281236_277970_281148_281368_279202; MCITY=-224%3A289%3A; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; BAIDUID_BFESS=F1CB82512F626E5BB8EC0A85AFD49E3A:FG=1; ZFY=qHLCIEVTaDMCcRD:Arc5NOxMD3TWeetnViLd:AYHSoSD0:C; BD_CK_SAM=1;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; channel=baidusearch; B64_BOT=1; H_PS_PSSID=39396_39530_39418_39590_39438_39526_39497_26350_39564; BA_HECTOR=ag0l2h8l200g8k2h8580al231ik4kj01q; COOKIE_SESSION=804_0_5_5_32_5_0_0_5_5_0_0_803_0_4_0_1698846087_0_1698846083%7C9%230_0_1698846083%7C1; H_PS_645EC=6090FviJT%2BOdjZ7SXkbJei12H%2BWAP2nxCIjU5rtdyuFQhST0bZx42mYstJM; BDSVRTM=181; baikeVisitId=f60332d2-5667-480d-a76d-6d09ee21f5fd'

}

proxy = '114.215.174.98:16817'

username = 'qjtwcvgt'
password = 'qjtwcvgt'

# 代理池
proxiesPool = [
    {"http": "http://%(user)s:%(pwd)s@%(proxy)s/" % {'user': username, 'pwd': password, 'proxy': proxy}},
    {"https": "https://%(user)s:%(pwd)s@%(proxy)s/" % {'user': username, 'pwd': password, 'proxy': proxy}}
]

proxies = random.choice(proxiesPool)

print(proxies)

# 请求对象的定制
req = urllib.request.Request(url=baidu_url, headers=headers)

# 模拟浏览器访问服务器
# res = urllib.request.urlopen(req)

# 1、获取handler对象
handler = urllib.request.ProxyHandler(proxies=proxies)

# 2、获取opener对象
opener = urllib.request.build_opener(handler)

# 3、调用open方法
res = opener.open(req)

# 获取响应的信息
content = res.read().decode('utf-8')

# 保存
with open('../temp/baidu_067.html', 'w', encoding='utf-8') as fp:
    fp.write(content)
